package info.androidhive.slidingmenu;

import info.androidhive.slidingmenu.MusicService.MusicBinder;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.widget.ImageButton;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class PlayingActivity extends Activity implements MediaPlayerControl{

	private MusicService musicSrv;
	private Intent playIntent;
	private boolean musicBound = false;
	public TextView songName, startTimeField, endTimeField;
	private MediaPlayer mediaPlayer;
	private double startTime = 0;
	private double finalTime = 0;
	private Handler myHandler = new Handler();;
	private int forwardTime = 5000;
	private int backwardTime = 5000;
	private SeekBar seekbar;
	private ImageButton playButton, pauseButton;
	public static int oneTimeOnly = 0;
	ArrayList<Songs> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_playing);
		
		list = new ArrayList<Songs>();
		setController();
		
	}

	public void play(View view) {
		Toast.makeText(getApplicationContext(), "Playing sound",
				Toast.LENGTH_SHORT).show();
		mediaPlayer.start();
		finalTime = mediaPlayer.getDuration();
		startTime = mediaPlayer.getCurrentPosition();
		if (oneTimeOnly == 0) {
			seekbar.setMax((int) finalTime);
			oneTimeOnly = 1;
		}

		endTimeField.setText(String.format(
				"%d min, %d sec",
				TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
				TimeUnit.MILLISECONDS.toSeconds((long) finalTime)
						- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
								.toMinutes((long) finalTime))));
		startTimeField.setText(String.format(
				"%d min, %d sec",
				TimeUnit.MILLISECONDS.toMinutes((long) startTime),
				TimeUnit.MILLISECONDS.toSeconds((long) startTime)
						- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
								.toMinutes((long) startTime))));
		
		seekbar.setProgress((int) startTime);
		myHandler.postDelayed(UpdateSongTime, 100);
		pauseButton.setEnabled(true);
		playButton.setEnabled(false);
	}

	private Runnable UpdateSongTime = new Runnable() {
		public void run() {
			startTime = mediaPlayer.getCurrentPosition();
			startTimeField.setText(String.format(
					"%d min, %d sec",
					TimeUnit.MILLISECONDS.toMinutes((long) startTime),
					TimeUnit.MILLISECONDS.toSeconds((long) startTime)
							- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
									.toMinutes((long) startTime))));
			seekbar.setProgress((int) startTime);
			myHandler.postDelayed(this, 100);
		}
	};

	public void pause(View view) {
		Toast.makeText(getApplicationContext(), "Pausing sound",
				Toast.LENGTH_SHORT).show();

		mediaPlayer.pause();
		pauseButton.setEnabled(false);
		playButton.setEnabled(true);
	}

	public void forward(View view) {
		int temp = (int) startTime;
		if ((temp + forwardTime) <= finalTime) {
			startTime = startTime + forwardTime;
			mediaPlayer.seekTo((int) startTime);
		} else {
			Toast.makeText(getApplicationContext(),
					"Cannot jump forward 5 seconds", Toast.LENGTH_SHORT).show();
		}

	}

	public void rewind(View view) {
		int temp = (int) startTime;
		if ((temp - backwardTime) > 0) {
			startTime = startTime - backwardTime;
			mediaPlayer.seekTo((int) startTime);
		} else {
			Toast.makeText(getApplicationContext(),
					"Cannot jump backward 5 seconds", Toast.LENGTH_SHORT)
					.show();
		}

	}

	/*
	 * Connect to the service
	 */
	private ServiceConnection musicConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			musicBound = false;

		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			MusicBinder binder = (MusicBinder) service;

			// Get service
			musicSrv = binder.getService();

			// Pass list
			Bundle extras = getIntent().getExtras();
			if (extras != null){
				list = (ArrayList<Songs>) extras.getSerializable("SongsList");
				musicSrv.setList(list);
			}
			

			musicBound = true;

		}
	};

	@Override
	public void start() {
		musicSrv.go();
		setController();

	}

	private void setController() {
		
		songName = (TextView) findViewById(R.id.textView4);
		startTimeField = (TextView) findViewById(R.id.textView1);
		endTimeField = (TextView) findViewById(R.id.textView2);
		
		seekbar = (SeekBar) findViewById(R.id.seekBar1);
		
		playButton = (ImageButton) findViewById(R.id.imageButton1);
		pauseButton = (ImageButton) findViewById(R.id.imageButton2);
		
		/*songName.setText("song.mp3");
		mediaPlayer = MediaPlayer.create(this, R.raw.song);*/
		
		seekbar.setClickable(false);
		pauseButton.setEnabled(false);
	}

	@Override
	public void pause() {
		musicSrv.pausePlayer();
		setController();
	}

	@Override
	public int getDuration() {
		if (musicSrv != null && musicBound)
			return musicSrv.getDur();
		// else
		return 0;
	}

	@Override
	public int getCurrentPosition() {
		if (musicSrv != null && musicBound)
		{
			return musicSrv.getPosn();
		}
		// else
		return 0;
	}

	@Override
	public void seekTo(int pos) {
		musicSrv.seek(pos);

	}

	@Override
	public boolean isPlaying() {
		if (musicSrv != null && musicBound)
			return musicSrv.isPng();
		// else
		return false;
	}

	@Override
	public int getBufferPercentage() {
		return 0;
	}

	@Override
	public boolean canPause() {

		return true;
	}

	@Override
	public boolean canSeekBackward() {

		return true;
	}

	@Override
	public boolean canSeekForward() {

		return true;
	}

	@Override
	public int getAudioSessionId() {
		return 0;
	}

}
